var gulp = require("gulp"),
    less = require("gulp-less"),
    minCss = require("gulp-clean-css"),
    connect = require("gulp-connect"),
    proxy = require('http-proxy-middleware'),
    express = require('express'),
    uglify = require("gulp-uglify"),
    babel = require("gulp-babel"),
    gutil = require("gulp-util"),
    del = require('del'),
    imagemin = require("gulp-imagemin"),
    concat = require("gulp-concat"),
    postcss = require('gulp-postcss'),
    htmlmin = require("gulp-htmlmin"),
    cssnext = require('cssnext'),
    precss = require('precss'),
    autoprefixer = require("gulp-autoprefixer");

//配置监听
gulp.task("watch", function(cb) {
    gulp.watch("src/**/*.less", ["cssHandle", "reload"]);
    gulp.watch("src/**/*.css", ["cssHandle", "reload"]);
    gulp.watch("src/**/*.js", ["scriptHandle", "reload"]);
    gulp.watch("src/**/*.{jpg,png,gif,ico}", ["imgHandle", "reload"]);
    gulp.watch("src/**/*.html", ["htmlHandle", "reload"]);
    del(['dest/**/*.js', 'dest/**/*.html', 'dest/**/*.css', 'dest/**/*.{jpg,png,gif,ico}'], cb);
});

//css相关处理
gulp.task("cssHandle", function() {
    var af = {
        browsers: ['last 2 versions', 'Android >= 4.0', "iOS 7"]
    };
    var processors = [autoprefixer, cssnext, precss];
    //文件夹打包
    gulp.src("src/**/*.css").pipe(postcss(processors)).pipe(autoprefixer(af)).pipe(minCss()).pipe(gulp.dest("dest"));
    gulp.src("src/**/*.less").pipe(less()).pipe(postcss(processors)).pipe(autoprefixer(af)).pipe(minCss()).pipe(gulp.dest("dest"));
});

//本地web服务器
gulp.task('server', function() {
    connect.server({
        root: 'dest',
        port: 8081,
        livereload: true,
        middleware: function(connect, opt) {
            return [
                proxy('/mq', {
                    target: 'http://192.168.30.106:8080',
                    changeOrigin: true,
                    pathRewrite: {
                        '^/mq': ''
                    }
                }),
                proxy('/haidong', {
                    target: 'http://ic.snssdk.com',
                    changeOrigin: true,
                    pathRewrite: {
                        '^/haidong': ''
                    }
                })
            ]
        }

    });
});

//处理js文件
gulp.task("scriptHandle", function() {
    //按文件夹打包
    gulp.src("src/**/*.js").pipe(babel({
        presets: ['es2015']
    })).pipe(uglify({
        mangle: { except: ['require', 'exports', 'module', '$'] } //排除混淆关键字
    })).on('error', function(err) {
        gutil.log(gutil.colors.red('[Error]'), err.toString());
    }).pipe(gulp.dest("dest"));
});

//html页面相关处理
gulp.task("htmlHandle", function() {
    var options = {
        removeComments: true, //清除HTML注释
        collapseWhitespace: true, //压缩HTML
        collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
        minifyJS: true, //压缩页面JS
        minifyCSS: true //压缩页面CSS
    };
    gulp.src("src/**/*.html").pipe(htmlmin(options)).pipe(gulp.dest("dest"));
});


//图片相关处理
gulp.task("imgHandle", function() {
    // gulp.src("src/**/*.{jpg,png,gif,ico}").pipe(imagemin()).pipe(gulp.dest("dest"));
});

//预处理相关
gulp.task("allHandle", ["htmlHandle", "cssHandle", "scriptHandle", "imgHandle"]);
//刷新页面
gulp.task("reload", function() {
    gulp.src("dest/**/*.html").pipe(connect.reload());
});
gulp.task("reload", function() {
    gulp.src("dest/**/*.css").pipe(connect.reload());
});
gulp.task("reload", function() {
    gulp.src("dest/**/*.{jpg,png,gif,ico}").pipe(connect.reload());
});
gulp.task("default", ["allHandle", "watch", "server", "imgHandle", "htmlHandle", "scriptHandle"]);